package com.ys.openapi.process;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.utils.HttpUtils;
import com.erp.utils.PropertiesUtils;
import com.erp.utils.WebApiUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 云枢工作流相关方法
 */
public class Process {
    //我的待办列表、分页查询 get方式
    public static  Object search_workitems(RequestDBList request,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("search_workitems");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url +request.toString();
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }
    //#分页查询，已办，get方式
    public  static Object list_finished_workitems(RequestYBList request ,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("list_finished_workitems");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url +request.toString();
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }
    //workflowName=&workflowCode=&originator=&unitType=&startTime=&endTime=&originatorName=&workflowTplName=&activityName=&participant=&participantName=&sequenceNo=&page=0&size=20
    //待阅 get方式
    public static  Object search_circulates(RequestDYList request,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("search_circulates");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url +request.toString();
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }

    //workflowName=&workflowCode=&originator=&unitType=&startTime=&endTime=&originatorName=&workflowTplName=&activityName=&participant=&participantName=&sequenceNo=&page=0&size=20
    //已阅 get方式
    public static  Object list_finished_circulates(RequestDYList request,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("list_finished_circulates");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url +request.toString();
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }
    //#我的流程，进行中 ，get方式
    //workflowName=&workflowCode=&unitType=&startTime=&endTime=&originatorName=&workflowTplName=&activityName=&participant=&participantName=&instanceState=PROCESSING,EXCEPTION,DRAFT&page=0&size=20
    //已完成
    //workflowName=&workflowCode=&unitType=&startTime=&endTime=&originatorName=&workflowTplName=&activityName=&participant=&participantName=&instanceState=COMPLETED&page=0&size=20
    //已作废
    //workflowName=&workflowCode=&unitType=&startTime=&endTime=&originatorName=&workflowTplName=&activityName=&participant=&participantName=&instanceState=CANCELED&page=0&size=20
    //instanceState=PROCESSING,EXCEPTION,DRAFT  进行中
    //instanceState=COMPLETED  已完成
    //instanceState=CANCELED   //已作废
   //我的流程,get方式
    // private int flag=2;//状态标记，0:已作废;1:已完成;2:进行中
    public static Object list_my_instances(RequestLCList request,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("list_my_instances");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url +request.toString();
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
   }

//http://47.100.197.8/api/api/runtime/workflow/list_workitem_approvals?workflowInstanceId=82bf5fdd4dea4f80811383c3ef22ce1a
    //根据流程实例id获取流程审批日志，
   public static  Object list_instance_logs(String workflowInstanceId,String access_token){
       String url= PropertiesUtils.loadProperties("openapi").getProperty("list_instance_logs");
       String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
       String path="http://"+ip+""+url +"?workflowInstanceId="+workflowInstanceId;
       String post = HttpUtils.doGet2(path,access_token,null);
       Object parse = JSON.parse(post);
       return parse;
   }
    public static  Object list_workitem_approvals(String workflowInstanceId,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("list_workitem_approvals");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url +"?workflowInstanceId="+workflowInstanceId;
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }
   //http://47.100.197.8/api/api/runtime/workflow/get_instance_baseinfo?workflowInstanceId=82bf5fdd4dea4f80811383c3ef22ce1a
    //根据流程实例id获取流程实例基础信息,可获取workitemId
    public  static  Object get_instance_baseinfo(String workflowInstanceId,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("get_instance_baseinfo");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url +"?workflowInstanceId="+workflowInstanceId;
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }
    //http://47.100.197.8/api/api/runtime/workflow/get_workflowtemplate_chart?workflowInstanceId=82bf5fdd4dea4f80811383c3ef22ce1a
    //成功调用获取流程图接口
    public  static  Object get_workflowtemplate_chart(String workflowInstanceId,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("get_workflowtemplate_chart");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url +"?workflowInstanceId="+workflowInstanceId;
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }
    //作废流程实例
    public static  Object abort_instance(String workflowInstanceId,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("abort_instance");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url +"?workflowInstanceId="+workflowInstanceId;
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }
    //结束流程实例
    public  static  Object finish_instance(String workflowInstanceId,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("finish_instance");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url +"?workflowInstanceId="+workflowInstanceId;
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }
    //#获取重放校验码,在表单提交时用，
    public  static  String getReplayToken(String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("getReplayToken");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url ;
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
       return  ((JSONObject)parse).getString("data");

    }
    //发起流程
    //public Object start_process(RequestStartProcess request,String id,Map<String,String>mapParams,String access_token){
    public static  Object start_process(RequestStartProcess request, String access_token){
        try {
        //获取防重放token
        String replayToken= getReplayToken(access_token);
        request.setReplayToken(replayToken);
        //获取业务对象，即流程输入参数
        /*BizObject bizObject=new BizObject();
        bizObject.setId(id);
        bizObject.setSchemaCode(request.getWorkflowCode());
        bizObject.setSheetCode(request.getWorkflowCode());
        mapParams.put("id",id);
        bizObject.setData(mapParams);
        request.setBizObject(bizObject);  在调用处赋值*/

        String url= PropertiesUtils.loadProperties("openapi").getProperty("start_process");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url ;
        //System.out.println(JSON.toJSONString(request));
        String post = null;
            System.out.println(JSON.toJSONString(request) );
         //post = HttpUtils.doPost5(path, JSON.toJSONString(request), access_token);

            post = HttpUtils.doPost2(path, JSON.toJSONString(request) , access_token);

          Object parse = JSON.parse(post);
            return parse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //提交,接口同发起是一个，不同参数
   // public Object commit(RequestCommitProcess request,String id,Map<String,String>mapParams,String access_token){
    public static Object commit(RequestCommitProcess request, String access_token){
        //获取防重放token
        String replayToken= getReplayToken(access_token);
        request.setReplayToken(replayToken);

        String url= PropertiesUtils.loadProperties("openapi").getProperty("start_process");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url ;
       // System.out.println(JSON.toJSONString(request));
        String post = null;

        //post = HttpUtils.doPost5(path, JSON.toJSONString(request), access_token);
        post = HttpUtils.doPost2(path, JSON.toJSONString(request).replace("\"null\"","null"), access_token);
        Object parse = JSON.parse(post);
        return parse;
        //获取业务对象，即流程输入参数
       /* BizObject bizObject=new BizObject();
        bizObject.setId(id);
        bizObject.setSchemaCode(request.getWorkflowCode());
        bizObject.setSheetCode(request.getWorkflowCode());
        mapParams.put("id",id);
        bizObject.setData(mapParams);
        request.setBizObject(bizObject);
        在调用处赋值
       */


//同意
  /*  {
             "workflowCode":"pay",
            "workItemId":"4a77b8e0b21c445b9c8fe4693f54c469",
            "workflowInstanceId":"4b9756c507f64658847e6ac532ac4ec6",
            "bizObject":{
                       "id":"0fd3e623453523542",
                         "data":{
                            "ShortText1628921911277":"44",
                             "id":"0fd3e623453523542",
                            "sequenceNo":"2021-001098"
                             },
                     "schemaCode":"pay",
                     "sheetCode":"pay",
                     "workflowInstanceId":"4b9756c507f64658847e6ac532ac4ec6"
                     },
             "approval":{
                     "content":"同意",
                      "workItemId":"4a77b8e0b21c445b9c8fe4693f54c469",
                     "workflowInstanceId":"4b9756c507f64658847e6ac532ac4ec6",
                     "workflowTokenId":"f9d43fb9119643ee938205c922e88608",//可为空
                     "activityCode":"Activity3",
                     "activityName":"审批",
                    "result":1
                    },
              "agree":true,
            "actionCode":"agree",
            "formType":"1",
            "replayToken":"REPLAYaeba3945c272468dbe82d6d31ba40c45"
    }*/

    }
    //#获取驳回节点的流程列表
    //http://47.100.197.8/api/api/runtime/workflow/list_reject_activities?workflowInstanceId=0dfca5fb18c44372b52ab4ab684bbfce&activityCode=Activity5
    public static  Object list_reject_activities(String workflowInstanceId,String activityCode,String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("list_reject_activities");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url+"?workflowInstanceId="+workflowInstanceId+"&activityCode="+activityCode ;
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return  ((JSONObject)parse).getString("data");
      /*  {
            "errcode":0,
                "errmsg":"成功根据流程实例id获取驳回页面的流程节点列表",
                "data":[
            {
                "activityCode":"Activity3",
                    "activityName":"审批",
                    "status":null,
                    "checkbox":true
            }
    ],
            "traceId":"02DE3B292FF9415D90BAF9CF2F452E4A"
        }*/
        //return null;
    }
    //驳回
    public static  Object reject_workItem(RequestRejectProcess request, String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("reject_workItem");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url ;
        // System.out.println(JSON.toJSONString(request));
        String post = null;

        //post = HttpUtils.doPost5(path, JSON.toJSONString(request), access_token);
        post = HttpUtils.doPost2(path, JSON.toJSONString(request).replace("\"null\"","null"), access_token);
        Object parse = JSON.parse(post);
        return parse;
       /* {
            "workflowCode":"pay",
                "workItemId":"cfa8f14df2b9455183566e50ac5182c4",
                "workflowInstanceId":"0dfca5fb18c44372b52ab4ab684bbfce",
                "bizObject":{
            "id":"95cf68e0d3c041948ef2781f442d37e4",
                    "data":{
                "ShortText1628921911277":"22",
                        "id":"95cf68e0d3c041948ef2781f442d37e4",
                        "sequenceNo":"2021-001099"
            },
                    "schemaCode":"pay",
                    "sheetCode":"pay",
                    "workflowInstanceId":"0dfca5fb18c44372b52ab4ab684bbfce"
        },
            "approval":{
            "content":"不同意",
                    "workItemId":"cfa8f14df2b9455183566e50ac5182c4",
                    "workflowInstanceId":"0dfca5fb18c44372b52ab4ab684bbfce",
                    "workflowTokenId":"5cab22b086dc44bd8641581badadc687",可为空
                    "activityCode":"Activity5",
                    "activityName":"付款实付",
                    "result":null
        },
            "rejectToActivityCode":"Activity3",
                "submitToReject":false,
                "formType":"1"
        }*/
        //return null;
    }
    //转阅 post
    public  static Object circulate_workItem(RequestCirculateProcess request, String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("circulate_workItem");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url ;
        String post = null;

        //post = HttpUtils.doPost5(path, JSON.toJSONString(request), access_token);
        post = HttpUtils.doPost2(path, JSON.toJSONString(request).replace("\"null\"","null"), access_token);
        Object parse = JSON.parse(post);
        return parse;
    }
    //转办,入参与转阅相同，不同之处在于转办只能是一人，转阅可以多人
    public static Object forward_workItem(RequestCirculateProcess request, String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("forward_workItem");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url ;
        String post = null;

        //post = HttpUtils.doPost5(path, JSON.toJSONString(request), access_token);
        post = HttpUtils.doPost2(path, JSON.toJSONString(request).replace("\"null\"","null"), access_token);
        Object parse = JSON.parse(post);
        return parse;
    }
    public static void main(String[] args) {
        WebApiUtils wau=new WebApiUtils();
        Map<String, String> token = wau.get_token("RKB006", "123456");
        System.out.println(token.get("access_token"));
        //启动流程
        //star(token.get("access_token"));
       RequestStartProcess rsp=new RequestStartProcess();
        rsp.setWorkflowCode("test_process");
       // rsp.setReplayToken(Process.getReplayToken(token.get("access_token")));
        rsp.setDepatmentId("2c2c80867ab40ee9017aca0657a30636");
        BizObject bizObject=new BizObject();
        bizObject.setId("0827147c3b0246049e4cddafaf1efff");
        bizObject.setSchemaCode("process_test1");
        bizObject.setSheetCode("process_test1");
        bizObject.setWorkflowInstanceId("null");
         BizData bd=new BizData();
        bizObject.setData(bd.toMap("type","1","0827147c3b0246049e4cddafaf1efff"));
        rsp.setBizObject(bizObject);
        System.out.println(JSON.toJSONString(rsp));
        System.out.println(JSON.toJSONString(JSON.toJSONString(rsp)));
        Process.start_process(rsp,token.get("access_token"));

        /////////////////////////////////////////////////////
       /* Process process=new Process();
       Object db=process.search_workitems(new RequestDBList(),token.get("access_token"));
        //process.list_finished_workitems(new RequestYBList(),token.get("access_token"));
        RequestLCList request=new RequestLCList();
        request.setFlag(1);
        //process.list_my_instances(request,token.get("access_token"));
        //http://47.100.197.8/form/detail?workitemId=452d83c7ca7f42f99f7771c87b029bee&workflowInstanceId=235b997fbad64238946c303b9c105ed9&return=%2Fworkflow-center%2Fmy-unfinished-workitem&workitemType=unfinishedWorkitem
      //  process.list_workitem_approvals("82bf5fdd4dea4f80811383c3ef22ce1a",token.get("access_token"));
      //  process.abort_instance("1df8cb4204334cb7bad615ed8d8245b8",token.get("access_token"));
      // System.out.println( process.getReplayToken(token.get("access_token")));
*/
      /*  RequestStartProcess rsp=new RequestStartProcess();
        rsp.setWorkflowCode("pay");
        rsp.setDepatmentId("2c2c80867ab40ee9017aca0657a30636");
        String tokenstring=token.get("access_token");
       // rsp.setReplayToken(process.getReplayToken(tokenstring));
       Map<String,String>mp= new HashMap<String,String>();
       mp.put("ShortText1628921911277","44444");
        Process.start_process(rsp,tokenstring);*/


       // Process.list_reject_activities("0dfca5fb18c44372b52ab4ab684bbfce","Activity5",token.get("access_token"));
    }

//发起工作流
///"deptId": "2c2c80867ab40ee9017aca0657a30636"
  /*  { "workflowCode": "pay",ssss
            "workItemId": null,
            "workflowInstanceId": null,
            "bizObject": {
                "id": "0fd3e6671adfasdfdasdf",
                "data": {
                         "ShortText1628921911277": "44",
                         "id": "0fd3e6671adfasdfdasdf"
                        },
                "schemaCode": "pay",
                "sheetCode": "pay",
                "workflowInstanceId": null
                       },
            "agree": true,
            "actionCode": "submit",
            "depatmentId": "2c2c80867ab40ee9017aca0657a30636",
            "formType": "1",
            "replayToken": "REPLAY4c402b5bd69d436086063146d803df26"
    }*/
/*{
    "actionCode": "submit",
        "agree": true,
        "bizObject": {
                         "id": "test1001",
                         "schemaCode": "pay",
                         "sheetCode": "pay"
                        },
         "depatmentId": "2c2c80867ab40ee9017aca0657a30636",
        "formType": "1",
        "replayToken": "REPLAYcd7915000415439dbbba01e284593ec2",
        "workflowCode": "pay"
}*/

    public static void star(String token){
        RequestStartProcess rsp=new RequestStartProcess();

    }
}
